// pages/mine/shop/mycard/detail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab_index: 0,
    card_id:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    var openid = app.globalData.openid;
    app.ihttpGet("https://www.58zltc.com/api.php/MyCard/getCard/openid/" + openid +"/card_id/" + options.id, function (res) {
      console.log(res.data.form);
      that.setData({
        list: res.data,
        input_list: res.data.form,
        add_url: res.data.form.add_url, 
        card_id: options.id
      })
    })
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  bindChange: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        list[idx].value = e.detail.value;
      }
    })
    this.setData({
      input_list: list
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formSubmit: function (e) {
    app.addFormId(e.detail.formId);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var post_data = e.detail.value;
    var data = this.data;
    let that = this;
    
    post_data.openid = app.globalData.openid;
    post_data.card_id =  data.card_id;
    if (data.type == 'edit') {
      post_data.id = data.info.id;
    }
    app.ihttpPost(data.add_url, post_data, function (res) {
      if (res.data.err_code > 0) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        wx.showToast({
          title: res.data.err_msg,
          icon: 'success'
        })
        setTimeout(function () {
          // wx.navigateBack({})
          wx.navigateTo({
            url: "/pages/mine/shop/mycard/cardlist",
          })
        }, 500)
      }
    })
  },
  
})